package com.xywy.bloodandroid.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {


	public static String makeDate(long time) {
		return makeDate(time + "");
	}

	/**
	 * 根据时间戳计算应该显示的时间
	 * 
	 * @param timestamp
	 * @return
	 */
	public static String makeDate(String timestamp) {
		// 此项目中传入的秒
		if (timestamp == null)
			return "";
		long timeLong = Long.parseLong(timestamp);
		timestamp = timeLong * 1000 + "";

		String correctTime = "";
		Calendar calendar = Calendar.getInstance();
		int currentDayInMonth = calendar.get(Calendar.DAY_OF_MONTH);
		int destDayInMonth = getDayInMonthOrWeek(timestamp, true);
		int dayInterval = currentDayInMonth - destDayInMonth;
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		switch (dayInterval) {
		case 0: // 一天之内显示14:30
			correctTime = format.format(Long.parseLong(timestamp));
			break;
		case 1: // 2天之内显示昨天
			correctTime = "昨天  " + format.format(Long.parseLong(timestamp));
			break;
		// 以下为超过2天的情况
		default:
			int destDayInWeek = getDayInMonthOrWeek(timestamp, false);
			int todayInWeek = getDayInMonthOrWeek(System.currentTimeMillis()
					+ "", false);
			if (dayInterval > 6) { // 显示年月日
				SimpleDateFormat genFormat = new SimpleDateFormat("yyyy-MM-dd");
				correctTime = genFormat.format(Long.parseLong(timestamp));
			} else {
				correctTime = makeWeekTime(destDayInWeek, todayInWeek)
						+ format.format(Long.parseLong(timestamp));
			}
			break;
		}
		return correctTime;
	}

	private static String makeWeekTime(int dayInWeek, int todayInWeek) {
		String weekTime = "";
		switch (dayInWeek) {
		case 1:
			weekTime = dayInWeek >= todayInWeek ? "上周一  " : "周一  ";
			break;
		case 2:
			weekTime = dayInWeek >= todayInWeek ? "上周二  " : "周二  ";
			break;
		case 3:
			weekTime = dayInWeek >= todayInWeek ? "上周三  " : "周三  ";
			break;
		case 4:
			weekTime = dayInWeek >= todayInWeek ? "上周四  " : "周四  ";
			break;
		case 5:
			weekTime = dayInWeek >= todayInWeek ? "上周五  " : "周五  ";
			break;
		case 6:
			weekTime = dayInWeek >= todayInWeek ? "上周六  " : "周六  ";
			break;
		case 0:
			weekTime = dayInWeek >= todayInWeek ? "上周日  " : "周日  ";
			break;
		}
		return weekTime;
	}

	/**
	 * 获取指定时间戳所在当月或者当周的天数
	 * 
	 * @param timestamp
	 * @return
	 */
	public static int getDayInMonthOrWeek(String timestamp, boolean isInMonth) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Long t = Long.parseLong(timestamp);
		String source = format.format(t);
		Date date;
		try {
			date = format.parse(source);
			return isInMonth ? date.getDate() : date.getDay();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * 转化成日期
	 * 
	 * @param timestamp
	 * @return
	 */
	public static String getData(String timestamp) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd ");
		String source = format.format(Double.valueOf(timestamp)*1000);
		return source;
	}
    
	/**
	 * 转换成时间 
	 * @return
	 */
	public  static  String getHourAndMinute(String timestamp){
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Long t = Long.parseLong(timestamp);
		String source = format.format(t);
		Date date ;
		try {
			date = format.parse(source);
			return date.getHours()+":"+date.getMinutes();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "-1";
	}
	
	
	/**
	 * 转换月：日 
	 * @return
	 */
	public  static  String getMouthAndDay(String timestamp){
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Long t = Long.parseLong(timestamp);
		String source = format.format(t);
		Date date ;
		try {
			date = format.parse(source);
			return date.getMonth()+"月"+date.getDay()+"日";
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "-1";
	}
	/**
	 * 附近的人时间显示，1小时之内显示几分钟前，超过1小时，显示几小时前
	 * 
	 * @param timestamp
	 *            秒
	 * @return
	 */
	public static String makeAroundPeopleTime(String timestamp) {
		if (timestamp == null)
			return "";
		long timeLong = Long.parseLong(timestamp);
		long currentTime = System.currentTimeMillis() / 1000;
		long one_minute = 60;
		long one_hour = 60 * 60;// 单位是秒
		long offset = currentTime - timeLong;
		if (offset <= one_minute) { // 小于1分钟
			return offset + "秒前";
		} else if (offset > one_minute && offset < one_hour) { // 把时间算为分钟
			return (offset / 60) + "分钟前";
		} else {
			return (offset / one_hour) + "小时前";
		}
	}
	public static String makeAroundPeopleTime(long timestamp) {
		long currentTime = System.currentTimeMillis() / 1000;
		long one_minute = 60;
		long one_hour = 60 * 60;// 单位是秒
		long offset = currentTime - timestamp;
		if (offset <= one_minute) { // 小于1分钟
			return "刚刚";
/*			return offset + "秒前";
*/		} else if (offset > one_minute && offset < one_hour) { // 把时间算为分钟
			return (offset / 60) + "分钟前";
		} else {
			return (offset / one_hour) + "小时前";
		}
	}

	/**
	 * 将时间戳转化为年月日,生成文件目录用
	 * 
	 * @param time
	 * @return
	 */
	public static String makeDateForDir(long time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		String d = format.format(time);
		return d;
	}
	public static String makeDateForTraceLog() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd");
		String d = format.format(System.currentTimeMillis());
		return d;
	}

	public static String makeDataForWeather() {
		SimpleDateFormat format = new SimpleDateFormat("E",
				Locale.CHINA);
/*		SimpleDateFormat format = new SimpleDateFormat("MM月dd日  E",
				Locale.CHINA);
*/		String d = format.format(System.currentTimeMillis());
		return d;
	}

	/**
	 * 获取天气界面的后5天的日期
	 * @return
	 */
	public static ArrayList<String> makeWeekForWeather() {
		String[] arr = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		calendar.setTime(date);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		
		ArrayList<String> list = new ArrayList<String>();
		
		int start = dayOfWeek;
		for (int i = 0; i < 4; i++) {
			if(start>=arr.length) start = 0;
			list.add(arr[start]);
			start++;
		}
		return list;
	}
    /**
     * 
     * @return
     */
	public static String  getMouthAndDays(String  time){
		System.out.println("获取的时间为多少呢 亲..."+time);
		Long value=Long.valueOf(time)*1000;
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(value);
		int m=calendar.get(Calendar.MONTH);
		int d=calendar.get(Calendar.DATE);
		System.out.println("获取的时间的转化...||"+m+"||.....||"+d);
		return (m+1)+"月"+d+"日";
	}
}
